package servlet;

import entity.OlahUser;
import entity.User;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import jpa.exceptions.NonexistentEntityException;

public class ServletUserHelper {

    HttpServletRequest request;

    public ServletUserHelper(HttpServletRequest request) {
        this.request = request;
    }

    public String signUp() {
        HttpSession session = request.getSession();
        User user = new User();
        OlahUser ou = new OlahUser();

        String username = request.getParameter("userName");
        String namaLengkap = request.getParameter("namaLengkap");
        String email = request.getParameter("email");
        String jenisKelamin = request.getParameter("jenisKelamin");
        String password = request.getParameter("pass");
        String temp = request.getParameter("umur");
        String kadarGula = request.getParameter("kadarGula");
        String beratBadan = request.getParameter("beratBadan");
        String lamaSakit = request.getParameter("lamaSakit");
        String catatan = request.getParameter("catatan");

        user.setUsername(username);
        user.setNamalengkap(namaLengkap);
        user.setEmail(email);
        user.setJeniskelamin(jenisKelamin);
        user.setPass(password);
        user.setKadargula(kadarGula);
        user.setBeratbadan(beratBadan);
        user.setLamasakit(lamaSakit);
        user.setCatatan(catatan);
        user.setLogStat(1);
        session.setAttribute("temRegis", user);
        if (!temp.equals("")) {
            try {
                int umur = Integer.parseInt(temp);
                user.setUmur(umur);
            } catch (NumberFormatException nfe) {
                session.setAttribute("gagal", "umur harus pakai angka");
                return "registrasi.jsp";
            }
        } else {
            session.setAttribute("gagal", "semua field harus terisi");
            return "registrasi.jsp";
        }
        session.setAttribute("temRegis", user);

        if (username.equals("") || namaLengkap.equals("") || email.equals("") || jenisKelamin.equals("") || password.equals("")
                || kadarGula.equals("") || username.equals("") || beratBadan.equals("") || lamaSakit.equals("") || catatan.equals("")) {
            session.setAttribute("gagal", "semua field harus terisi");
            return "registrasi.jsp";
        } else {
            if (password.length() > 4) {
                boolean hasilCheck = ou.check(username);
                if (!hasilCheck) {
                    ou.addUser(user);
                    session.removeAttribute("gagal");
                    session.removeAttribute("temRegis");
                    return "sudahRegistrasi.jsp";
                } else {
                    session.setAttribute("gagal", "user sudah ada");
                    return "registrasi.jsp";
                }
            } else {
                session.setAttribute("gagal", "semua field harus terisi");
                return "registrasi.jsp";
            }
        }
    }

    public String bukaLogin() {
        HttpSession session = request.getSession();
        User user = new User();
        OlahUser ou = new OlahUser();
        if (session.getAttribute("username") != null) {
            String username = (String) session.getAttribute("username");
            boolean hasilCheck = ou.check(username);
            if (hasilCheck) {
                user = ou.getUser(username);
                request.setAttribute("user", user);
                return "profil.jsp";

            } else {
                return "userTakDitemukan.jsp";
            }
        } else {
            return "formlogin.jsp";
        }
    }

    public String submitLogin() {
        HttpSession session = request.getSession();
        User user = new User();
        OlahUser ou = new OlahUser();
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (username.equals("") || password.equals("")) {
            return "fieldLoginKosong.jsp";
        } else {
            boolean hasilCheck = ou.check(username);
            if (hasilCheck) {
                user = ou.getUser(username);
                if (username.equals(user.getUsername()) && password.equals(user.getPass())) {
                    session.setAttribute("username", user);
                    if (user.getLogStat() == 1) {
                        request.setAttribute("pesanLogin", "Selamat Anda berhasil Login");
                        return "profil.jsp";
                    } else {
                        return "profilAdmin.jsp";
                    }
                } else {
                    return "index.jsp";
                }
            } else {
                return "userTakDitemukan.jsp";
            }
        }
    }

    public String logout() {
        HttpSession session = request.getSession();
        session.removeAttribute("username");
        session.invalidate();
        return "index.jsp";
    }

    public String bukaEditProfil() {
        HttpSession session = request.getSession();
        User user = new User();
        OlahUser ou = new OlahUser();

        if (session.getAttribute("username") != null) {
            User uname = (User) session.getAttribute("username");
            String username = uname.getUsername();
            boolean hasilCheck = ou.check(username);
            if (hasilCheck) {
                user = ou.getUser(username);
                request.setAttribute("user", user);
                return "editProfil.jsp";
            } else {
                return "userTakDitemukan.jsp";
            }
        } else {
            return "sudahLogout.jsp";
        }
    }

    public String submitEditProfil() {
        HttpSession session = request.getSession();
        User user = new User();
        OlahUser ou = new OlahUser();

        String password = request.getParameter("pass");
        String kadarGula = request.getParameter("kadarGula");
        String beratBadan = request.getParameter("beratBadan");

        if (session.getAttribute("username") != null) {
            User uname = (User) session.getAttribute("username");
            String username = uname.getUsername();
            if (password.length() > 4) {
                boolean hasilCheck = ou.check(username);
                if (hasilCheck) {
                    user = ou.getUser(username);
                    user.setPass(password);
                    user.setKadargula(kadarGula);
                    user.setBeratbadan(beratBadan);
                    ou.editUser(user);
                    request.setAttribute("user", user);
                    return "profil.jsp";
                } else {
                    return "userTakDitemukan.jsp";
                }
            } else {
                return "passMin5Edit.jsp";
            }
        } else {
            return "sudahLogout.jsp";
        }
    }

    public String hapusUser() throws NonexistentEntityException {
        HttpSession session = request.getSession();
        String idUser = request.getParameter("idUser");
        List<User> user = new ArrayList<User>();
        OlahUser ou = new OlahUser();

        if (session.getAttribute("username") != null) {
            if (idUser == null) {
                user = ou.getUsers();
                request.setAttribute("users", user);
            } else {
                Long id = Long.parseLong(idUser);
                ou.deleteUser(id);
                user = ou.getUsers();
                request.setAttribute("users", user);
            }
            return "usersList.jsp";
        } else {
            return "sudahLogout.jsp";
        }
    }

    public String tentangKami() {
        return "aboutus.jsp";
    }
}